package gui;

import classes.Evento;
import classes.EventoParticipante;
import classes.Servidor;
import erro.ConexaoException;
import erro.ConsultaException;
import erro.GeralException;
import fachada.Fachada;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import org.apache.commons.mail.EmailException;
import rn.Email;

/**
 *
 * @author Roberta
 */
public class EventoMostrar extends javax.swing.JFrame {

    public static int id;
    DefaultListModel dlm = new DefaultListModel();
    DefaultListModel dlm2 = new DefaultListModel();

    public EventoMostrar() {
        initComponents();
        this.setLocationRelativeTo(null);
        textObs.setEditable(false);
        textEvento.setEditable(false);
        textHora.setEditable(false);
        textLocal.setEditable(false);
        textData.setEnabled(false);
        btSalvar.setVisible(false);
        listServidores.setVisible(false);
        checkEncerrar.setVisible(false);
        paneBt.setVisible(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        textObs = new javax.swing.JTextArea();
        lbData = new javax.swing.JLabel();
        lbHora = new javax.swing.JLabel();
        lbEvento = new javax.swing.JLabel();
        lbLocal = new javax.swing.JLabel();
        textData = new com.toedter.calendar.JDateChooser();
        textHora = new javax.swing.JTextField();
        try{
            javax.swing.text.MaskFormatter hora= new javax.swing.text.MaskFormatter("##:##");
            textHora = new javax.swing.JFormattedTextField(hora);
        }
        catch (Exception e){
        }
        textEvento = new javax.swing.JTextField();
        textLocal = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        list2 = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        listServidores = new javax.swing.JList();
        paneBt = new javax.swing.JPanel();
        btAdd = new javax.swing.JButton();
        btRetira = new javax.swing.JButton();
        btAddAll = new javax.swing.JButton();
        btRetiraAll = new javax.swing.JButton();
        checkEncerrar = new javax.swing.JCheckBox();
        jLabel7 = new javax.swing.JLabel();
        lbStatus = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btExcluir = new javax.swing.JButton();
        btEmail = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Detalhes do Evento");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Data:");

        jLabel2.setText("Hora:");

        jLabel3.setText("Evento:");

        jLabel4.setText("Local:");

        jLabel5.setText("Participantes:");

        jLabel6.setText("Obs.:");

        textObs.setColumns(20);
        textObs.setFont(new java.awt.Font("Tahoma", 0, 12));
        textObs.setRows(5);
        jScrollPane3.setViewportView(textObs);

        textEvento.setColumns(2);

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        list2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane2.setViewportView(list2);

        listServidores.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane1.setViewportView(listServidores);

        btAdd.setText(">");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });

        btRetira.setText("<");
        btRetira.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRetiraActionPerformed(evt);
            }
        });

        btAddAll.setText(">>");
        btAddAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddAllActionPerformed(evt);
            }
        });

        btRetiraAll.setText("<<");
        btRetiraAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRetiraAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout paneBtLayout = new javax.swing.GroupLayout(paneBt);
        paneBt.setLayout(paneBtLayout);
        paneBtLayout.setHorizontalGroup(
            paneBtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(btAddAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btRetira, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
            .addComponent(btRetiraAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
        );
        paneBtLayout.setVerticalGroup(
            paneBtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneBtLayout.createSequentialGroup()
                .addComponent(btAdd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btRetira)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btAddAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btRetiraAll)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(paneBt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(paneBt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        checkEncerrar.setText("Encerrar Evento");

        jLabel7.setText("Status:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(38, 38, 38)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel4)
                                .addComponent(jLabel3)
                                .addComponent(jLabel1)))
                        .addComponent(jLabel5)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(textData, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(lbData)
                                                .addGap(161, 161, 161)
                                                .addComponent(lbHora))
                                            .addComponent(lbEvento)
                                            .addComponent(lbLocal)))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(39, 39, 39)
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(textHora, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(textEvento, javax.swing.GroupLayout.PREFERRED_SIZE, 417, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textLocal, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(checkEncerrar, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbStatus)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(lbData))
                        .addComponent(lbHora)
                        .addComponent(textData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(textHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lbEvento)
                    .addComponent(textEvento))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lbLocal)
                    .addComponent(textLocal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(lbStatus))
                .addGap(8, 8, 8)
                .addComponent(checkEncerrar)
                .addContainerGap())
        );

        btExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/excluir.png"))); // NOI18N
        btExcluir.setText("Excluir");
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });

        btEmail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/email.png"))); // NOI18N
        btEmail.setText("Reenviar e-mail");
        btEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEmailActionPerformed(evt);
            }
        });

        btAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/editar.png"))); // NOI18N
        btAlterar.setText("Alterar");
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/salvar.png"))); // NOI18N
        btSalvar.setText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(btExcluir, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
            .addComponent(btEmail, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btAlterar, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
            .addComponent(btSalvar, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(btExcluir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btEmail)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btAlterar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btSalvar)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        int selectedOption = JOptionPane.showConfirmDialog(null, "Deseja realmente excluir o evento?", "Alerta!", JOptionPane.YES_NO_OPTION);
        if (selectedOption == JOptionPane.YES_OPTION) {
            Evento e = new Evento();
            EventoParticipante ep = new EventoParticipante();
            Fachada f = new Fachada();
            e.setEvento_id(id);
            try {
                f.deletarEvento(e, ep);
                JOptionPane.showMessageDialog(null, "Evento excluído!");
                this.dispose();
            } catch (ConexaoException ex) {
                JOptionPane.showMessageDialog(null, "Não foi possível conectar-se ao banco de dados!\n" + ex);
            } catch (ConsultaException ex) {
                JOptionPane.showMessageDialog(null, "Não foi possível excluir o Evento!\n" + ex);
            }
        } else {
            this.repaint();
        }
    }//GEN-LAST:event_btExcluirActionPerformed

    private void btEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEmailActionPerformed
        int selectedOption = JOptionPane.showConfirmDialog(null, "Deseja realmente reenviar e-mail?", "Sistema informa:", JOptionPane.YES_NO_OPTION);
        if (selectedOption == JOptionPane.YES_OPTION) {
            reEmail();
        } else {
            this.repaint();
        }
    }//GEN-LAST:event_btEmailActionPerformed

    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
        carregarList();
        Evento e = new Evento();
        Fachada f = new Fachada();
        e.setEvento_id(EventoMostrar.id);
        Evento evento = null;
        try {
            evento = f.pesquisar(e);
        } catch (ConexaoException ex) {
        } catch (ConsultaException ex) {
        }
        textObs.setEditable(true);
        textEvento.setEditable(true);
        textHora.setEditable(true);
        textLocal.setEditable(true);
        textData.setEnabled(true);
        btSalvar.setVisible(true);
        if (evento.getStatus() != 2) {
            checkEncerrar.setVisible(true);
        } else if (evento.getStatus() == 2) {
            checkEncerrar.setText("Abrir Evento");
            checkEncerrar.setVisible(true);
        }
        paneBt.setVisible(true);
        listServidores.setVisible(true);

    }//GEN-LAST:event_btAlterarActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        Fachada f = new Fachada();
        if (textData.getDate() == null) {
            JOptionPane.showMessageDialog(null, "Digite uma data!");
            textData.grabFocus();
        }
        try {
            Evento e = new Evento();
            Fachada fachada = new Fachada();
            Servidor s = new Servidor();
            EventoParticipante ep = new EventoParticipante();
            e.setEvento(textEvento.getText().trim());
            e.setLocal(textLocal.getText().trim());
            e.setObs(textObs.getText().trim());
            e.setData(textData.getDate());
            if (checkEncerrar.isSelected() && checkEncerrar.getText().equals("Encerrar Evento")) {
                e.setStatus(2);
            } else if (checkEncerrar.isSelected() && checkEncerrar.getText().equals("Abrir Evento")) {
                e.setStatus(1);
            } else if (!checkEncerrar.isSelected()) {
                if (lbStatus.getText().equals("Evento encerrado.")) {
                    e.setStatus(2);
                } else if (lbStatus.getText().equals("Evento aberto.")) {
                    e.setStatus(1);
                }
            }
            DateFormat sdfHora = new SimpleDateFormat("HH:mm");
            String hora = textHora.getText();
            e.setHora((java.util.Date) sdfHora.parse(hora));
            sdfHora.setLenient(false);
            s = new Servidor();
            ArrayList lista = new ArrayList();
            ArrayList<Servidor> listaServidores = fachada.listar(s);
            DefaultListModel modelol = (DefaultListModel) list2.getModel();
            for (int l = 0; l < modelol.getSize(); l++) {
                String servAtual = modelol.getElementAt(l).toString();
                for (Servidor serv : listaServidores) {
                    if (serv.getNome().equals(servAtual)) {
                        lista.add(serv);
                    }
                }
            }
            e.setEvento_id(EventoMostrar.id);
            ep.setEvento_id(e);
            ep.setParticipante(lista);
            f.alterarEvento(e, ep);
            this.dispose();
        } catch (ConexaoException e) {
            JOptionPane.showMessageDialog(null, "Não foi possível conectar-se ao banco de dados!\n" + e);
        } catch (ConsultaException e) {
            JOptionPane.showMessageDialog(null, "Não foi possível alterar o Evento!\n" + e);
        } catch (GeralException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
            this.repaint();
        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(null, "Hora inválida!");
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
        dlm = (DefaultListModel) list2.getModel();
        if (listServidores.getSelectedValue() == null) {
            JOptionPane.showMessageDialog(null, "Selecione pelo menos um Servidor.");
        } else {
            for (int i = 0; i < listServidores.getSelectedValuesList().size(); i++) {
                dlm.addElement(listServidores.getSelectedValue());
            }
            list2.setModel(dlm);
            int[] selecao = listServidores.getSelectedIndices();
            for (int i = selecao.length; i > 0; i--) {
                int j = listServidores.getSelectedIndex();
                ((DefaultListModel) listServidores.getModel()).remove(j);
            }
        }
    }//GEN-LAST:event_btAddActionPerformed

    private void btRetiraAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRetiraAllActionPerformed
        int indices = list2.getModel().getSize() + 1;
        final int[] vetor = new int[indices];
        for (int i = 0; i < indices; i++) {
            vetor[i] = i;
        }
        list2.setSelectedIndices(vetor);
        dlm2 = (DefaultListModel) listServidores.getModel();
        for (int i = 0; i < list2.getSelectedValuesList().size(); i++) {
            dlm2.addElement(list2.getModel().getElementAt(i));
        }
        listServidores.setModel(dlm2);

        int[] selecao = list2.getSelectedIndices();
        for (int i = selecao.length; i > 0; i--) {
            ((DefaultListModel) list2.getModel()).remove(0);
        }
    }//GEN-LAST:event_btRetiraAllActionPerformed

    private void btAddAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddAllActionPerformed
        int indices = listServidores.getModel().getSize() + 1;
        final int[] vetor = new int[indices];
        for (int i = 0; i < indices; i++) {
            vetor[i] = i;
        }
        listServidores.setSelectedIndices(vetor);
        dlm = (DefaultListModel) list2.getModel();
        for (int i = 0; i < listServidores.getSelectedValuesList().size(); i++) {
            dlm.addElement(listServidores.getModel().getElementAt(i));
        }
        list2.setModel(dlm);

        int[] selecao = listServidores.getSelectedIndices();
        for (int i = selecao.length; i > 0; i--) {
            ((DefaultListModel) listServidores.getModel()).remove(0);
        }
    }//GEN-LAST:event_btAddAllActionPerformed

    private void btRetiraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRetiraActionPerformed
        if (list2.getSelectedValue() == null) {
            JOptionPane.showMessageDialog(null, "Selecione pelo menos um Servidor.");
        } else {
            dlm2 = (DefaultListModel) listServidores.getModel();
            for (int i = 0; i < list2.getSelectedValuesList().size(); i++) {
                int j = list2.getSelectedIndex();
                dlm2.addElement(list2.getSelectedValue());
            }
            listServidores.setModel(dlm2);

            int[] selecao = list2.getSelectedIndices();
            for (int i = selecao.length; i > 0; i--) {
                int j = list2.getSelectedIndex();
                ((DefaultListModel) list2.getModel()).remove(j);
            }
        }
    }//GEN-LAST:event_btRetiraActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EventoMostrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EventoMostrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EventoMostrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EventoMostrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new EventoMostrar().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btAddAll;
    public javax.swing.JButton btAlterar;
    public javax.swing.JButton btEmail;
    public javax.swing.JButton btExcluir;
    private javax.swing.JButton btRetira;
    private javax.swing.JButton btRetiraAll;
    private javax.swing.JButton btSalvar;
    private javax.swing.JCheckBox checkEncerrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lbData;
    private javax.swing.JLabel lbEvento;
    private javax.swing.JLabel lbHora;
    private javax.swing.JLabel lbLocal;
    private javax.swing.JLabel lbStatus;
    private javax.swing.JList list2;
    private javax.swing.JList listServidores;
    private javax.swing.JPanel paneBt;
    private com.toedter.calendar.JDateChooser textData;
    private javax.swing.JTextField textEvento;
    private javax.swing.JTextField textHora;
    private javax.swing.JTextField textLocal;
    private javax.swing.JTextArea textObs;
    // End of variables declaration//GEN-END:variables

    public void carregarTabela() {
        try {
            Fachada f = new Fachada();
            Evento e = new Evento();
            EventoParticipante ep = new EventoParticipante();
            ArrayList<EventoParticipante> listaP = (ArrayList) f.listarEP();
            e.setEvento_id(EventoMostrar.id);
            Evento lista = f.pesquisar(e);
            DateFormat formatHora = new SimpleDateFormat("HH:mm");
            Date horaE = lista.getHora();
            String hora = (formatHora.format(horaE));
            if (lista.getStatus() == 2) {
                lbStatus.setText("Evento encerrado.");
            } else {
                lbStatus.setText("Evento aberto.");
            }
            textEvento.setText(lista.getEvento());
            textObs.setText(lista.getObs());
            textObs.setWrapStyleWord(true);
            textObs.setLineWrap(true);
            textData.setDate(lista.getData());
            textHora.setText(hora);
            textLocal.setText(lista.getLocal());
            DefaultListModel modeloList = new DefaultListModel();
            for (int i = 0; i < listaP.size(); i++) {
                ep.setEvento_id(listaP.get(i).getEvento_id());
                int p = ep.getEvento_id().getEvento_id();
                if (p == lista.getEvento_id()) {
                    modeloList.addElement(listaP.get(i).getNome());
                }
                list2.setModel(modeloList);
            }
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, "Não foi possível conectar-se ao banco de dados!\n" + ex);
        } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, "Ocorreu um erro ao tentar carregar a tela.\n" + ex);
        }
    }

    public void reEmail() {
        Fachada f = new Fachada();
        Evento e = new Evento();
        Servidor s = new Servidor();
        e.setEvento_id(EventoMostrar.id);
        ArrayList<EventoParticipante> listaEp;

        try {
            listaEp = (ArrayList) f.listarEP();
            ArrayList<Servidor> lista = new ArrayList();
            Evento evento = f.pesquisar(e);
            ArrayList<Servidor> listaServidores = f.listar(s);

            for (int l = 0; l < listaEp.size(); l++) {
                String servAtual = listaEp.get(l).getNome();
                for (Servidor serv : listaServidores) {
                    if (serv.getNome().equals(servAtual)) {
                        lista.add(serv);
                    }
                }
            }
            String nomes = "";
            String emails = "";
            String separador = "";

            for (int n = 0; n < lista.size(); n++) {
                if (listaEp.size() != 1) {
                    if (n + 1 == lista.size()) {
                        separador = "";
                    } else {
                        separador = ", ";
                    }
                }
                nomes += lista.get(n).getNome() + separador;
                if (lista.get(n).getEmail() == 1) {
                    emails += lista.get(n).getEmailPessoal().toLowerCase().trim() + ",";
                }
                emails += lista.get(n).getEmailProf().toLowerCase().trim() + separador;
            }

            DateFormat formatdata = new SimpleDateFormat("dd/MM/yyyy");
            Date data = evento.getData();
            String datA = (formatdata.format(data));
            DateFormat formatHora = new SimpleDateFormat("HH:mm");
            Date horaE = evento.getHora();
            String hora = (formatHora.format(horaE));

            for (int i = 0; i < lista.size(); i++) {
                Email.assunto = evento.getEvento();
                Email.conteudo = "Data: " + datA + "\n" + "Hora: " + hora + "\n" + "Local: " + evento.getLocal()
                        + "." + "\n" + "Obs.: " + evento.getObs() + "."
                        + "." + "\n" + "Participantes: " + nomes + ".";
            }
            Email.destino = emails;
            Email email = new Email();
            email.enviaEmail();
            JOptionPane.showMessageDialog(null, "Enviado com sucesso!");
            textObs.grabFocus();
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, "Não foi possível conectar-se ao banco de dados!\n" + ex);
        } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, "Erro:\n" + ex);
        } catch (EmailException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    public void carregarList() {
        Fachada f = new Fachada();
        Servidor s = new Servidor();

        int indices = list2.getModel().getSize();
        final int[] vetor = new int[indices];
        for (int i = 0; i < indices; i++) {
            vetor[i] = i;
        }
        list2.setSelectedIndices(vetor);
        DefaultListModel modelo = new DefaultListModel();
        DefaultListModel modelo2 = new DefaultListModel();
        ArrayList<Servidor> lista = new ArrayList<Servidor>();
        ArrayList lista2 = new ArrayList();
        try {
            lista = f.listar(s);
            lista2 = (ArrayList) list2.getSelectedValuesList();
            for (int i = 0; i < lista.size(); i++) {
                modelo.addElement(lista.get(i).getNome());
            }
            for (int i = 0; i < lista2.size(); i++) {
                String atual = (String) lista2.get(i);
                for (Servidor serv : lista) {
                    if (serv.getNome().toLowerCase().trim().equals(atual.toLowerCase().trim())) {
                        modelo2.addElement(serv.getNome());
                        modelo.removeElement(serv.getNome());
                    }
                }
            }
            listServidores.setModel(modelo);
            list2.setModel(modelo2);
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, "Não foi possível conectar-se ao banco de dados!\n" + ex);
        } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, "Ocorreu um erro ao tentar carregar a lista de Servidores.\n" + ex);
        }
    }
}
